import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReentrantLockTest {
    public static void main(String[] args) throws InterruptedException {
//        ReentrantLock lock = new ReentrantLock();
//        //加锁
//        lock.lock();
//        //被上锁的代码
//
//        //释放锁
//        lock.unlock();
//        //尝试获取锁，如果获取到锁返回true，执行加锁的逻辑，如果获取不到锁返回false，执行不加锁的逻辑
//        lock.tryLock();
//        //尝试加锁，并指定等待时间
//        lock.tryLock(1, TimeUnit.SECONDS);
//
//        //创建一个公平锁    获取锁的方式遵循先来后到
//        ReentrantLock lock1 = new ReentrantLock(true);
//        //创建一个读写锁
//        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
//
//        //创建一个读锁
//        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
//
//        //创建一个写锁
//        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

        //唤醒与休眠机制
        ReentrantLock lockA = new ReentrantLock();
        Condition male = lockA.newCondition();
        Condition female = lockA.newCondition();

        //根据不同阻塞条件进行阻塞等待
        male.await();
        male.signal();
        male.signalAll();
        female.await();
        female.signal();
        female.signalAll();
    }
}
